package com.pixel.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.pixel.game.controls.InputHandler;
import com.pixel.game.entity.UnitManager;

public class PixelGame extends ApplicationAdapter {
    private ShapeRenderer shapeRenderer;
    private UnitManager unitManager;
    private InputHandler inputHandler;
    private Rectangle selectionRectangle;

    private BitmapFont font;
    private SpriteBatch batch;
    private Stage stage;
    private Skin skin;
    private TextButton restartButton;
    private TextButton startButton; // 定义开始按钮

    private boolean gameStarted = false; // 添加游戏启动标志

    @Override
    public void create() {
        shapeRenderer = new ShapeRenderer();
        font = new BitmapFont(); // 使用默认字体，可以加载自定义字体文件
        batch = new SpriteBatch();
        unitManager = new UnitManager();
        selectionRectangle = new Rectangle();

        stage = new Stage(new ScreenViewport());

        // 创建输入多路复用器并将 stage 和 inputHandler 添加进去
        InputMultiplexer inputMultiplexer = new InputMultiplexer();
        inputMultiplexer.addProcessor(stage);
        inputHandler = new InputHandler(unitManager, selectionRectangle);
        inputMultiplexer.addProcessor(inputHandler);

        // 设置输入处理器为多路复用器
        Gdx.input.setInputProcessor(inputMultiplexer);
        // 初始化皮肤
        skin = new Skin(Gdx.files.internal("uiskin.json")); // 使用默认皮肤

        // 初始化开始按钮
        startButton = new TextButton("Start Game", skin);
        startButton.setDebug(true); // 启用调试边框
        startButton.setSize(200, 60); // 设置按钮尺寸
        startButton.setPosition(Gdx.graphics.getWidth() / 2 - 100, Gdx.graphics.getHeight() / 2 + 50); // 确保位置合适
        stage.addActor(startButton);

        startButton.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                startGame(); // 点击按钮后启动游戏
            }
        });

        // 初始化重启按钮
        restartButton = new TextButton("Restart", skin);
        restartButton.setPosition(Gdx.graphics.getWidth() / 2 - 50, Gdx.graphics.getHeight() / 2);
        restartButton.setVisible(false); // 初始时设置为不可见

        restartButton.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                restartGame();
            }
        });

        stage.addActor(restartButton);


    }

    private void startGame() {
        unitManager.initializeUnits(); // 初始化游戏单位
        startButton.setVisible(false); // 隐藏开始按钮
        gameStarted = true; // 设置游戏启动标志
    }

    private void restartGame() {
        unitManager.restartGame(); // 重置游戏
        gameStarted = true; // 保持游戏启动状态
        startGame(); // 直接开始游戏
    }


    @Override
    public void render() {
        float deltaTime = Gdx.graphics.getDeltaTime();

        if (gameStarted) {
            // 更新单位的位置
            unitManager.updateUnits(deltaTime);
            // 判断是否重启游戏
            restartButton.setVisible(unitManager.getFriendlyUnits().isEmpty());
        }
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // 绘制所有单位
        unitManager.drawUnits(shapeRenderer,font, batch);

        shapeRenderer.end();

        // 绘制选择框
        inputHandler.drawSelectionRectangle(shapeRenderer);

        // 绘制 UI
        stage.act(deltaTime);
        stage.draw();
    }

    @Override
    public void dispose() {
        font.dispose();
        batch.dispose();
        shapeRenderer.dispose();
        stage.dispose();
        skin.dispose();
    }
}
